package com.junlong0716.enjoylearning.ui

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.widget.TextView
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.junlong0716.enjoylearning.CommentPresenter
import com.junlong0716.enjoylearning.MyApp
import com.junlong0716.enjoylearning.R
import com.junlong0716.enjoylearning.adapter.CommentAdapter
import com.junlong0716.enjoylearning.constant.NetConstant
import com.junlong0716.enjoylearning.entity.CommentEntity
import com.junlong0716.enjoylearning.net.api.NetApi
import com.junlong0716.getknowledgedemo.decoration.DividerItemNoEndDecoration
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_topic_comment.*
import kotlinx.android.synthetic.main.view_title_bar.*
import org.json.JSONObject
import java.util.ArrayList

class TopicCommentActivity : AppCompatActivity() {
    private var topicId: Int = 0

    private lateinit var commentData: ArrayList<CommentEntity.DataBean.ListBean>
    private lateinit var commentAdapter: CommentAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_topic_comment)

        topicId = intent.getIntExtra("topic_id", 0)
        rl_back.setOnClickListener {
            finish()
        }

        tv_title.text = "专题评论"

        requestData()
        commentData = ArrayList()

        rv_list.layoutManager = LinearLayoutManager(this)
        commentAdapter = CommentAdapter(commentData, R.layout.item_comment)
        rv_list.adapter = commentAdapter
        rv_list.addItemDecoration(DividerItemNoEndDecoration(this, DividerItemNoEndDecoration.VERTICAL_LIST))



        fl_conform.setOnClickListener {
            val detComment = edt_comment.text.toString().trim()
            if (detComment == "") {

                ToastUtils.showShort("请输入评论内容！")

                return@setOnClickListener
            }
            requestAddComment(detComment)
        }


        commentAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.ll_love_container -> {
                    val map =  HashMap<String,Any>()
                    map["userId"] = MyApp.instance().userAccount!!.id
                    map["commentId"] = commentData[position].id
                    val jsonObject = JSONObject(map)

                    OkGo.post<String>(NetConstant.REQUEST_HEADER + NetApi.API_TOPIC_COMMENT_ADD_PRAISE)
                            .tag(this)
                            .upJson(jsonObject)
                            .execute(object :StringCallback(){
                                override fun onSuccess(response: Response<String>?) {
                                    val jsonObj = JSONObject(response!!.body())
                                    val code = jsonObj.getString("code")
                                    if (code == "200"){
                                        requestData()
                                    }
                                }
                            })
//                    val commentPresenter = CommentPresenter()
//                    commentPresenter.requestAddCommentPraise(commentData[position].id, this, object : CommentPresenter.AddCommentPraiseCallback {
//                        override fun requestAddCommentPraiseSucceed() {
//                            requestData()
//                        }
//
//                        override fun requestAddCommentPraiseFailed() {
//
//                        }
//                    })
                }
            }
        }
    }

//    private fun requestCommentData() {
//        val commentPresenter = CommentPresenter()
//        commentPresenter.requestComment(this, 1, 20, videoId, object : CommentPresenter.RequestCommentCallback {
//            override fun requestCommentSucceed(commentData: ArrayList<CommentEntity.DataBean.ListBean>) {
//                if (commentData.isEmpty()) {
//                    val emptyView = LayoutInflater.from(this@VideoActivity).inflate(R.layout.view_empty, null)
//                    emptyView.findViewById<TextView>(R.id.tv_empty_content).text = "还没有人评论呢~ 快来抢沙发吧！"
//                    commentAdapter.emptyView = emptyView
//                } else {
//                    this@VideoActivity.commentData.clear()
//                    this@VideoActivity.commentData.addAll(commentData)
//                    commentAdapter.notifyDataSetChanged()
//                }
//            }
//        })
//    }

    private fun requestAddComment(detComment: String) {
        val map = HashMap<String, Any>()
        map["userId"] = MyApp.instance().userAccount!!.id
        map["specialId"] = topicId
        map["commentContent"] = detComment

        val json = JSONObject(map)
        OkGo.post<String>(NetConstant.REQUEST_HEADER + NetApi.API_TOPIC_COMMENT)
                .tag(this)
                .upJson(json)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        val jsonObj = JSONObject(response!!.body())
                        val code = jsonObj.getString("code")
                        if (code == "200") {
                            edt_comment.setText("")
                            requestData()
                        }
                    }
                })
    }

    private fun requestData() {
        val map = HashMap<String, Any>()
        map["specialId"] = topicId
        val json = JSONObject(map)
        OkGo.post<String>(NetConstant.REQUEST_HEADER + NetApi.API_TOPIC_COMMENT_QUERY)
                .tag(this)
                .upJson(json)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        val gson = Gson()
                        val commentEntity = gson.fromJson<CommentEntity>(response!!.body(), CommentEntity::class.java)
                        if (commentEntity.code == "200") {
                            if (commentEntity.data.list.isEmpty()) {
                                val emptyView = LayoutInflater.from(this@TopicCommentActivity).inflate(R.layout.view_empty, null)
                                emptyView.findViewById<TextView>(R.id.tv_empty_content).text = "还没有人评论呢~ 快来抢沙发吧！"
                                commentAdapter.emptyView = emptyView
                            } else {
                                this@TopicCommentActivity.commentData.clear()
                                this@TopicCommentActivity.commentData.addAll(commentEntity.data.list)
                                commentAdapter.notifyDataSetChanged()
                            }
                        }
                    }
                })
    }

    override fun finish() {
        super.finish()
        this.overridePendingTransition(0, R.anim.anim_down)
    }

}
